package com.dq.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dq.health.dao.drug.DrugMapper;
import com.dq.health.dao.user.UserBaseInfoMapper;
import com.dq.health.dto.request.drug.QueryDrugParam;
import com.dq.health.dto.request.drug.SaveDrugRequest;
import com.dq.health.dto.response.drug.DrugInfo;
import com.dq.health.entity.drug.Drug;
import com.dq.health.service.IAPIDrugService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <>
 *
 * @Author: chenxin
 * @Date: 2021/5/17
 */
@Service
public class DrugServiceImpl implements IAPIDrugService {

    @Resource
    DrugMapper drugMapper;

    @Resource
    UserBaseInfoMapper userBaseInfoMapper;

    @Override
    public Long addDrug(SaveDrugRequest request) {
        Drug drug = new Drug();
        drug.setDrugCode(request.getDrugCode());
        drug.setDrugName(request.getDrugName());
        drug.setDrugNum(request.getDrugNum());
        drug.setDrugPrice(request.getDrugPrice());
        drug.setCreateUserId(request.getOperationUserId());
        drug.setCreateTime(new Date());

        drugMapper.insert(drug);
        return drug.getDrugId();
    }

    @Override
    public void updateDrug(SaveDrugRequest request) {
        Drug drug = new Drug();
        drug.setDrugId(request.getDrugId());
        drug.setDrugName(request.getDrugName());
        drug.setDrugNum(request.getDrugNum());
        drug.setDrugPrice(request.getDrugPrice());
        drug.setUpdateUserId(request.getOperationUserId());
        drug.setUpdateTime(new Date());

        drugMapper.updateById(drug);
    }

    @Override
    public Drug getDrugById(Long drugId) {
        return drugMapper.selectById(drugId);
    }

    @Override
    public Drug getDrugByCode(String drugCode) {
        return drugMapper.selectOne(new QueryWrapper<Drug>().lambda().eq(Drug::getDrugCode, drugCode));
    }

    @Override
    public DrugInfo getDrugInfo(Long drugId) {
        Drug drug = drugMapper.selectById(drugId);
        DrugInfo drugInfo = new DrugInfo();
        drugInfo.setDrugId(drug.getDrugId());
        drugInfo.setDrugCode(drug.getDrugCode());
        drugInfo.setDrugName(drug.getDrugName());
        drugInfo.setDrugPrice(drug.getDrugPrice());
        drugInfo.setDrugNum(drug.getDrugNum());
        drugInfo.setCreateUserId(drug.getCreateUserId());
        drugInfo.setCreateTime(drug.getCreateTime());
        drugInfo.setCreateUserName(userBaseInfoMapper.selectById(drug.getCreateUserId()).getUserName());
        return drugInfo;
    }

    @Override
    public IPage<DrugInfo> queryDrugListByParam(QueryDrugParam param) {
        IPage<DrugInfo> drugInfoPage = new Page<>(param.getPage(), param.getPageSize());

        return null;
    }
}
